﻿using System.Net;
using AutoMapper;
using B.S.DataBase.MyProject.Domain;
using B.S.DataBase.MyProject.Domain.Storages;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using MediatR;
using T.E.ERP.Write.API.Applicantions.Command.StoragesCommand;

namespace T.E.ERP.Write.API.Applicantions.CommandHandler.StoragesCommandHandler
{
    public class AddWhereHorseHandler : IRequestHandler<AddWhereHorseCommand, APIResult<object>>
    {
        private readonly IBaseRepository<BaseStorage> _baseStorage;
        private readonly IBaseRepository<SystemLog> _systemLog;
        private IMapper mapper;

        public AddWhereHorseHandler(IBaseRepository<BaseStorage> baseStorage, IMapper mapper, IBaseRepository<SystemLog> systemLog)
        {
            _baseStorage = baseStorage;
            this.mapper = mapper;
            _systemLog = systemLog;
        }

        public async Task<APIResult<object>> Handle(AddWhereHorseCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();
            result.Code = APIEnum.添加成功;
            result.Msg = APIEnum.添加成功.ToString();
            try
            {
                //仓库表
                var entity = mapper.Map<BaseStorage>(request);
                await _baseStorage.Add(entity);

                //实例化系统日志表
                SystemLog systemlog = new SystemLog();
                systemlog.LoginName = "赵策";
                systemlog.LoginTime = DateTime.Now;
                systemlog.Dosomething = "添加了一条仓库信息";
                await _systemLog.Add(systemlog);

                result.data = entity;
            }
            catch (Exception ex)
            {
                result.Code = APIEnum.添加失败;
                result.Msg = APIEnum.添加失败.ToString() + ex.Message;
            }

            return await Task.FromResult(result);
        }
    }
}
